// pages/login/login.js
import Toast from '@vant/weapp/toast/toast';
import {request} from '../../utils/request.js';
import {Encrypt} from '../../utils/util.js';
Page({
  nameInput(e){
    this.data.name = e.detail.value
  },
  passInput(e){
    this.data.password = e.detail.value
  },
  data: {
    choseLoginRole:0,//默认教师端
    errShow:false,
    pasLogin:true,
    name:'',
    password:'',
  },
  //切换角色
  roleChange(data){
    this.setData({
      choseLoginRole:data.detail.index
    })
  },
  //切换快捷登录
  tagLogin(){
    this.setData({
      pasLogin: !this.data.pasLogin
    })
  },
  //登录
  toLogin(){
    if(!this.data.name ||!this.data.password){
      Toast.fail('请输入账号密码');
      return
    }
    let vm = this
    request({
      url:'/review-paper/weixin/login',
      data:{
        userName: this.data.name.trim(),
        // password: Encrypt(this.data.password.trim())
        password: this.data.password.trim()
      },
      success (res) {
        if(res.data.status == 1){
            wx.setStorage({
              key:"token",
              data:res.data.data.token
            })
            wx.setStorage({
              key:'nameAndPas',
              data:vm.data.name.trim() + '|||'+vm.data.password.trim()
            })
            //保存登陆的端
            res.data.data.choseLoginRole = vm.data.choseLoginRole
            wx.setStorage({
              key:"userInfo",
              data:res.data.data
            })
            getApp().globalData.header.accessToken = res.data.data.token
            let isTeacher,isGuardian//教师，监护人
            res.data.data.roles.forEach(val => {
              if(val.roleCode<9){
                isTeacher = true
              }
              if(val.roleCode==9){
                isGuardian = true
              }
            })
            if(vm.data.choseLoginRole == 0){
              if(isTeacher){
                wx.switchTab({
                  url: '../index/index',
                })
              }else{
                Toast('该账户没有教师信息')
              }
            }else{
              if(isGuardian){
                wx.redirectTo({
                  url: '/pages/studentScore/studentScore', // 目标页面的路径
                });
              }else{
                Toast('该账户没有家长信息')
              }
            }
           
        }else{
          Toast.fail(res.data.msg);
        }
      }
    })
  },
  bindgetphonenumber(e){
    request({
      url: '/review-paper/weixin/loginByPhoneNumber',
      data:{
        code:e.detail.code
      },
      success (res) {
        if(res.data.status == 1){
          wx.setStorage({
            key:"token",
            data:res.data.data.token
          })
          wx.setStorage({
            key:"userInfo",
            data:res.data.data
          })
          getApp().globalData.header.accessToken = res.data.data.token
          wx.switchTab({
            url: '../index/index',
          })
        }else{
          Toast.fail(res.data.msg);
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    let vm = this
    wx.getStorage({
      key: 'nameAndPas',
      success (res) {
        if(res.data){
          let arr = res.data.split('|||')
          vm.data.name = arr[0]
          vm.data.password = arr[1]
          vm.setData({
            name:vm.data.name,
            password:vm.data.password
          })
        }
      },
      fail: function(err) {}
    })
    wx.getStorage({
      key: 'userInfo',
      success (res) {
        if(res.data){
          if(res.data.token){
            let isGuardian//监护人
            res.data.roles.forEach(val => {
              if(val.roleCode==9){
                isGuardian = true
              }
            })
            if(isGuardian && res.data.choseLoginRole == 1){
              wx.redirectTo({
                url: '/pages/studentScore/studentScore', // 目标页面的路径
              });
            }else{
              wx.switchTab({
                url: '../index/index',
              })
            }
          }
        }
      },
      fail: function(err) {}
    })
  },
  //跳转视频号
  navigateToChannel: function() {
    wx.openChannelsUserProfile({
      finderUserName:getApp().globalData.sphId,
      complete(e){
        console.log(e)
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})